//
//  MainTabBar.swift
//  MQWeibo
//
//  Created by SYN on 15/11/11.
//  Copyright © 2015年 SYN. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let btnCount = 5
        
        let w = self.bounds.size.width / CGFloat(btnCount)
        
        let h = self.bounds.size.height
        //每个按钮的大小固定
        let frame = CGRectMake(0, 0, w, h)
        //指定数组中的类型都是UIView,在oc中所有控件都继承自UIView
        var index = 0
        
        for v in subviews {
            if v is UIControl && !(v is UIButton){
                v.frame = CGRectOffset(frame, CGFloat(index) * w, 0)
                
//                index++
//                
//                if index == 2 {
//                    index++
//                }
                index += (index == 1) ? 2 : 1
            }
        }
        //指定撰写按钮的位置
//        self.composedButton.frame = frame
//        
//        self.composedButton.center = CGPoint(x: self.center.x, y: h * 0.5)
        
        self.composedButton.frame = CGRectOffset(frame, 2 * w, 0)
    }
    
    lazy var composedButton:UIButton = {
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)

        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        //懒加载的代码是闭包,是提前准备好的代码,在需要的时候进行,闭包中就需要self.
        self.addSubview(btn)
        
        return btn
        
        }()
    
}
